<!-- 新增/修改弹框 -->
<template>
  <!-- 新增/修改弹窗 -->
  <el-dialog
    v-model="dialogAddOrEdit.visible"
    :width="900"
    :title="dialogAddOrEdit.title"
    draggable
    close-icon="CloseBold"
    @close="closeDialog(false)"
  >
    <el-form ref="addOrEditFormRef" v-loading="loading" :model="formData" :rules="rules" label-width="170" size="">
      <!-- 添加保证金账户 -->
      <el-row justify="start" class="d_left">
        <el-col :span="12" class="">
          <el-form-item label="项目名称" prop="projectId">
            <el-input v-model="selectedAccount.projectName" disabled placeholder="请选择" />
            <el-button type="primary" plain style="position: absolute; right: -15px" @click="handleSelectAccount"
              >选择</el-button
            >
          </el-form-item>
        </el-col>
        <el-col :span="12" class="">
          <el-form-item label="缴纳主体名称" prop="paymentName">
            <el-input v-model="selectedAccount.paymentName" disabled placeholder="自动关联" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="">
          <el-form-item label="缴纳主体统一社会信用代码" prop="paymentCode">
            <el-input v-model="selectedAccount.paymentCode" disabled placeholder="自动关联" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="">
          <el-form-item label="账户名称" prop="guaranteeAccountName">
            <el-input v-model="selectedAccount.guaranteeAccountName" disabled placeholder="自动关联" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="">
          <el-form-item label="账户编号" prop="guaranteeAccountCode">
            <el-input v-model="selectedAccount.guaranteeAccountCode" disabled placeholder="自动关联" />
          </el-form-item>
        </el-col>
        <el-col v-show="selectedAccount.guaranteeAccountType == 1" :span="12" class="">
          <el-form-item label="缴存类型" prop="depositeTypeName">
            <el-input v-model="selectedAccount.depositeTypeName" disabled placeholder="自动关联" />
          </el-form-item>
        </el-col>
        <!-- <el-col :span="12" class="">
          <el-form-item label="动用通知书文号" prop="notificationLetter">
            <el-input v-model="formData.notificationLetter" placeholder="请输入动用通知文书号" />
          </el-form-item>
        </el-col> -->
        <el-col :span="12" class="">
          <el-form-item label="收入编号" prop="code">
            <el-input v-model="formData.code" placeholder="请输入收入编号" />
          </el-form-item>
        </el-col>

        <el-col :span="12" class="">
          <el-form-item label="交易类型" prop="tradingType">
            <el-select v-model="formData.tradingType" placeholder="请选择" disabled clearable>
              <el-option v-for="item in tradingType" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="">
          <el-form-item label="收入金额" prop="amount">
            <!-- <el-input v-model="formData.amount" placeholder="请输入收入金额" /> -->
            <money-input v-model="formData.amount" unit="元"></money-input>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="">
          <el-form-item label="收支日期" prop="payDate">
            <el-date-picker
              v-model="formData.payDate"
              type="date"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
              placeholder="选择日期"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <select-guarantee-account :dialog-select="dialogAccount" @select-send-close="sendClose"></select-guarantee-account>
    <template #footer>
      <div class="dialog_footer">
        <el-button type="primary" :loading="loading" @click="handleSubmit">{{ dialogAddOrEdit.footBtn }}</el-button>
        <el-button @click="closeDialog(false)">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import MoneyInput from "@/components/MoneyInput/index.vue";
import { ProjectGuaranteeIncomeEditForm } from "@/api/guarantee/types";
import SelectGuaranteeAccount from "@/views/common/SelectProjectGuaranteeAccount/index.vue";
import { getDicts } from "@/store/modules/dict";

import { CreateProjectGuaranteeIncome, GetProjectGuaranteeIncome } from "@/api/guarantee/index";
import { FormRules } from "element-plus";

const props = defineProps({
  dialogAddOrEdit: {
    type: Object,
    default: () => ({
      visible: false,
      title: "创建保证金账户",
      footBtn: "核实账户信息",
      id: 0,
      type: 1,
    }),
  },
});

const { dialogAddOrEdit } = toRefs(props);

const dialogAccount = ref({
  visible: false,
});

const emit = defineEmits(["sendClose"]);

const loading = ref(false);
const formData = reactive<ProjectGuaranteeIncomeEditForm>({
  tradingType: 100,
});
const addOrEditFormRef = ref(ElForm);
const tradingType = getDicts("tradingType");
const selectedAccount = ref<any>({});

const rules = reactive<FormRules<ProjectGuaranteeIncomeEditForm>>({
  //添加保证金
  projectId: [{ required: true, message: "请选择项目", trigger: "blur" }],
  code: [{ required: true, message: "请输入收入编号", trigger: "blur" }],
  amount: [{ required: true, message: "请输入收入金额", trigger: "blur" }],
  payDate: [{ required: true, message: "请选择收入日期", trigger: "blur" }],
  tradingType: [{ required: true, message: "请选择交易类型", trigger: "blur" }],
});

const handleSelectAccount = function () {
  dialogAccount.value.visible = true;
};

const sendClose = function (info: any) {
  dialogAccount.value.visible = false;
  if (info) {
    selectedAccount.value = info;
    formData.guaranteePaymentId = info.id;
    formData.depositeType = info.depositeType;
    formData.projectId = info.projectId;
    if (info.guaranteeAccountType == 1) {
      formData.incomeWay = 100;
    } else if (info.guaranteeAccountType == 2) {
      formData.incomeWay = 102;
    } else if (info.guaranteeAccountType == 3) {
      formData.incomeWay = 103;
    } else if (info.guaranteeAccountType == 4) {
      formData.incomeWay = 101;
    } else {
      formData.incomeWay = 104;
    }
  }
};

//初始化表单
const initForm = () => {
  if (dialogAddOrEdit.value.id) {
    loading.value = true;
    GetProjectGuaranteeIncome(dialogAddOrEdit.value.id)
      .then((res) => {
        if (res.success) {
          Object.assign(formData, res.data);
        } else {
        }
      })
      .finally(() => {
        loading.value = false;
      });
  }
};

watch(
  () => dialogAddOrEdit.value.visible,
  () => {
    if (dialogAddOrEdit.value.visible) {
      initForm();
    }
  }
);

/**
 * 关闭弹窗
 */
function closeDialog(query: boolean, data?: any) {
  emit("sendClose", query, data);
  resetForm();
}

/**
 * 重置表单
 */
function resetForm() {
  addOrEditFormRef.value.resetFields();
  addOrEditFormRef.value.clearValidate();
  formData.id = undefined;
}

/**
 * 项目表单提交
 */
function handleSubmit() {
  addOrEditFormRef.value.validate((valid: any) => {
    if (valid) {
      loading.value = true;
      CreateProjectGuaranteeIncome(formData)
        .then((res) => {
          if (res.success) {
            closeDialog(true);
          } else {
          }
        })
        .finally(() => (loading.value = false));
    }
  });
}
</script>

<style lang="scss" scoped>
.d_left {
  margin-left: 10px;
}
:deep(.el-form-item) {
  flex-wrap: wrap;
}
.dialog_footer {
  // display: flex;
  // justify-content: center;
}

:deep(.el-input) {
  width: 80%;
}

:deep(.el-select) {
  width: 100%;
}

:deep(.el-input__wrapper) {
  width: 100%;
}

:deep(.el-form-item__label) {
  color: #696868;
  font-size: 13px;
}
</style>
